package com.bi.onlineEducation.web.controller;

import com.bi.onlineEducation.annotation.WebProcess;
import com.bi.onlineEducation.dao.PurchaseDao;
import com.bi.onlineEducation.domain.PurchasePo;
import com.bi.onlineEducation.exception.BusinessHandleException;
import com.bi.onlineEducation.service.PurchaseService;
import com.bi.onlineEducation.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Author: chenYuTao
 * @Time: 2022/1/6 15:44
 * @Description: 基础数据控制层
 **/
@RestController
@Api(tags = "购买信息控制层", description = "购买信息数据接口")
public class PurchaseController {

    @Resource
    private PurchaseService purchaseService;
    @Resource
    private PurchaseDao purchaseDao;

    @ApiOperation("查询所有")
    @WebProcess
    @GetMapping(value = "/purchase/findAll")
    public ResultCode findAll() {
        return ResultCode.success(purchaseService.findAll());
    }

    @ApiOperation("根据id查询购买信息")
    @WebProcess
    @GetMapping(value = "/purchase/findById")
    public ResultCode findById(@RequestParam("id") Integer id) {
        return ResultCode.success(purchaseService.findById(id));
    }

    @ApiOperation("根据lid查询购买信息")
    @WebProcess
    @GetMapping(value = "/purchase/findAllBylId")
    public ResultCode findAllBylId(@RequestParam("lId") Integer lId) {
        return ResultCode.success(purchaseDao.findAllBylId(lId));
    }

    @ApiOperation("订单搜索所有")
    @WebProcess
    @GetMapping(value = "/purchase/search")
    public ResultCode search(@RequestParam("cName") String cName, @RequestParam("address") String address) {
        return ResultCode.success(purchaseService.search(cName, address));
    }

    @ApiOperation("包含当前用户订单搜索")
    @WebProcess
    @GetMapping(value = "/purchase/searchBylId")
    public ResultCode searchBylId(@RequestParam("cName") String cName, @RequestParam("address") String address
            , @RequestParam("lId") Integer lId) {
        return ResultCode.success(purchaseService.searchBylId(cName, address, lId));
    }

    @ApiOperation("保存信息")
    @WebProcess
    @PostMapping(value = "/purchase/save")
    public ResultCode save(@RequestBody PurchasePo purchasePo) {
        Optional.ofNullable(purchasePo.getCId()).orElseThrow(() -> BusinessHandleException.buildBizException("订单购买时未获取到课程编号"));
        Optional.ofNullable(purchasePo.getLId()).orElseThrow(() -> BusinessHandleException.buildBizException("订单购买时未获取到购买人编号"));
        List<PurchasePo> allBylId = purchaseDao.findAllBylId(purchasePo.getLId());
        if (allBylId.stream().filter(vo -> ObjectUtils.equals(vo.getCId(), purchasePo.getCId())).count() >= 1) {
            throw BusinessHandleException.buildBizException("该课程已购买");
        }
        purchasePo.setPCreateTime(new Date());
        return ResultCode.success(purchaseService.save(purchasePo));
    }

    @ApiOperation("根据id删除购买信息")
    @WebProcess
    @DeleteMapping(value = "/purchase/deleteById")
    public ResultCode deleteById(@RequestParam("id") Integer id) {
        return ResultCode.success(purchaseService.deleteById(id));
    }

    @ApiOperation("批量根据id删除购买信息")
    @WebProcess
    @DeleteMapping(value = "/purchase/deleteByIds")
    public ResultCode deleteById(@RequestParam("ids") List<Integer> ids) {
        return ResultCode.success(purchaseService.deleteByIds(ids));
    }


}